package com.ruoyi.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.DingUser;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.mapper.DingUserMapper;
import com.ruoyi.pojo.DingResposeUser;
import com.ruoyi.service.DingService;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Classname DingServiceimpl
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/4/25 21:04
 * @Created by m元波
 */
@Service
public class DingServiceimpl implements DingService {

    @Autowired
    DingUserMapper dingUserMapper;
    @Autowired
    SysUserServiceImpl sysUserService;
    @Autowired
    SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    SysLoginService loginService;
    @Override
    @Transactional
    public String getToken(JSONObject user) {
        String dingId= (String) user.get("userid");
        DingUser dingUser = dingUserMapper.getDingUserByDingId(dingId);
        DingResposeUser dingResposeUser = JSONObject.parseObject(user.toJSONString(), DingResposeUser.class);
        //检测是否存在钉钉用户
        if (dingUser==null){
            dingUser=new DingUser();
            //通过手机号查询系统用户
            SysUser sysUser=sysUserService.selectUserByPhone(dingResposeUser.getMobile());
            if (sysUser==null){
                //添加用户
                Long userId=insertUser(dingResposeUser);
                sysUser=new SysUser();
                sysUser.setUserId(userId);

            }
            //与钉钉用户绑定
            dingUser.setName(dingResposeUser.getName());
            dingUser.setDingId(dingId);
            dingUser.setMobile(dingResposeUser.getMobile());
            dingUser.setTitle("");
            dingUser.setUserId(sysUser.getUserId());
            if (dingUserMapper.insert(dingUser)!=1){
                return null;
            }
        }
//        LoginUser loginUser=tran(dingUser);
        String token = loginService.ddlogin(dingUser.getName(), "123456");
        return token;
    }

    private Long insertUser(DingResposeUser dingUser) {
        SysUser sysUser=new SysUser();
        sysUser.setNickName(dingUser.getName());
        sysUser.setUserName(dingUser.getName());
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setPhonenumber(dingUser.getMobile());
        boolean b = sysUserService.registerUser(sysUser);
        if (b){
            //添加用户角色
            SysUserRole sysUserRole=new SysUserRole();
            sysUserRole.setRoleId(2l);
            sysUserRole.setUserId(sysUser.getUserId());
            sysUserRoleMapper.insertRoleUser(sysUserRole);
        }
        return sysUser.getUserId();
    }

    private LoginUser tran(DingUser dingUser) {

        return null;
    }
}

